package cn.rui.controller.core;

import cn.rui.common.aspects.Log;
import cn.rui.common.utils.IpUtiles;
import cn.rui.controller.error.ControllerExceptionAdvice;
import cn.rui.pojo.User;
import cn.rui.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @author 徽州大都督
 * @date 2021/4/16
 */
@Controller
@RequestMapping("/user")
@Api(value="权限验证controller",tags={"用户登录验证"})
public class UserController extends ControllerExceptionAdvice {

    @Autowired
    private UserService userService;

    /**
     * 用于security跳转到首页,记录登录用户日志
     * @param session
     * @return
     */
    @RequestMapping("/jumpIndex")
    @Log("登录成功")
    @ApiOperation(value="登录跳转", notes="登录跳转")
    public String jumpIndex(HttpSession session) {
        //记录用户登陆操作
        //SysLog sysLog=new SysLog();
        //sysLog.setUsername(SecurityContextHolder.getContext().getAuthentication().getName());
        //sysLog.setCreateDate(new Date());
        HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        //sysLog.setIp(IpUtiles.getRealIp(request));
        //sysLog.setOperation("登陆成功");
        //sysLog.setMethod("login");
        //sysLogService.saveSysLog(sysLog);
        String username = SecurityContextHolder.getContext ().getAuthentication ().getName ();
        System.out.println ("用户登录成功："+ username);
        System.out.println ("当前ip："+ IpUtiles.getRealIp (request));
        return "redirect:"+request.getContextPath()+"/user/index";
    }

//    @RequestMapping({"login"})
//    //@Log("登录")
//    public String admin(HttpSession session){
//        return "login";
//    }

    @RequestMapping("/index")
    @Log("访问主页")
    public String index(){

        //throw new MyExcetion ("测试自定义异常");
        return "index";
    }

    @RequestMapping("list")
    @PreAuthorize ("hasAnyRole('list','admin')")
    @Log("访问列表页面")
    public String list(){
        return "userList";
    }

    @RequestMapping("add")
    @Log("访问添加页面")
    @PreAuthorize ("hasAnyRole('add','admin')")
    @ResponseBody
    public String add(){
        return "add";
    }

    @RequestMapping("register")
    @ApiOperation(value="注册账户", notes="注册账户")
    public String register(@Valid User user){

        Integer register = userService.register (user);
        if (register>0){
            return "login";
        }
            return "403";
    }
}
